package com.dshore.messagecenter.dao.topicqueue;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dshore.messagecenter.model.basedata.MdApp;
import com.dshore.messagecenter.model.topicqueue.MdTopicAccess;
import com.dshore.messagecenter.util.UUIDUtil;

/**
 * 源应用
 *
 */
@Repository
public class TopicAccessDao {

	@Autowired
	JdbcTemplate jdbcTemplate;

	
	
	/**
	 * 根据编码查询源应用
	 * @param topicQueueNo
	 * @return
	 */
	public List<MdApp> queryOriAppByQueueSign(String topicQueueNo) {
		String sql = " SELECT ID,APP_NAME from T_MD_APP where ID in " + 
				"( SELECT ACCESS_APPID FROM T_MD_TOPIC_ACCESS where TOPIC_QUEUE_NO = ? and DEL_FLAG = 0)";
		RowMapper<MdApp> rowMapper = new BeanPropertyRowMapper<>(MdApp.class);
		return jdbcTemplate.query(sql,rowMapper, topicQueueNo);
	}
	/**
	 * 查询接入方式
	 */
	public List<MdTopicAccess> queryByTopicSign(String topicQueueNo) {
		String sql = " SELECT * from T_MD_TOPIC_ACCESS where TOPIC_QUEUE_NO = ? ";
		RowMapper<MdTopicAccess> rowMapper = new BeanPropertyRowMapper<>(MdTopicAccess.class);
		return jdbcTemplate.query(sql, rowMapper, topicQueueNo);
	}
	
	/**
	 * 查询1个源应用(根据accessAppId查询一个)
	 */
	public MdApp queryOriApp(String accessAppId) {
		String sql = " SELECT ID,APP_NAME appName,QUEUE_KEY from T_MD_APP where ID = ?";
		RowMapper<MdApp> rowMapper = new BeanPropertyRowMapper<>(MdApp.class);
		return jdbcTemplate.queryForObject(sql, rowMapper, accessAppId);
	}
	
	/**
	 * 保存源应用
	 */
	public Integer saveOriApp(String id, int delFlag, String topicQueueNo, String originId) {
		String sql ="insert into T_MD_TOPIC_ACCESS (ID,CREATED_TIME,MODIFIED_TIME,"
				+ " DEL_FLAG,TOPIC_QUEUE_NO,ACCESS_APPID) " 
				+ "values (?, ?, ?, ?, ?, ? ) ";
		return jdbcTemplate.update(sql,id,new Date(),new Date(),delFlag,topicQueueNo,originId);
	}
	
	/**
	 * 遍历删除access
	 */
	public int deleteAccessByQueueSigns(String[] qSigns) {
	   final List<String> updateParams=new ArrayList<>();
	   for(String qSign : qSigns) {
		   updateParams.add(qSign);
		}
		String sql= "update T_MD_TOPIC_ACCESS set DEL_FLAG=1, MODIFIED_TIME=? where TOPIC_QUEUE_NO =?";	
		int[] execute = jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));//第一个传数据库的时间
				ps.setString(2, updateParams.get(i));
			}			
			@Override
			public int getBatchSize() {
				return updateParams.size();
			}
		});
		return execute.length;
	}

//新增服务接入
	public Integer saveService(MdTopicAccess mdTopicAccess) {
		String sql= "insert into T_MD_TOPIC_ACCESS (ID,ACCESS_APPID,TOPIC_QUEUE_NO,"+
				"DEL_FLAG,CREATED_TIME,MODIFIED_TIME,ACCESS_URL)"
				+"values(?,?,?,?,?,?,?)";
		return jdbcTemplate.update(sql,UUIDUtil.getUUId(),mdTopicAccess.getAccessAppid(),
				mdTopicAccess.getTopicQueueNo(),0,new Date(),
				new Date(),mdTopicAccess.getAccessUrl());
	}

}
